package com.infoblazer.grailsmaps

import grails.web.JSONBuilder

class CityController {

  def scaffold = true


  def show = {

    def city = City.get(params.id)

    if (params.format == 'json') {
      buildCityJson(params.jsoncallback, city)

    }
    else {
      [city: city, stateId: params.stateId]
    }

  }


  private void buildCityJson(jsoncallback, showCity) {
    def builder = new JSONBuilder()

    def result = builder.build {

      id = showCity.id
      cityName = showCity.cityName
      longitude = showCity.longitude
      latitude = showCity.latitude
      state = {
        id = showCity.state.id
        stateAbbrev = showCity.state.stateAbbrev
        stateName= showCity.state.stateName
      }

    }
    if (jsoncallback){
    render jsoncallback + "(" + result.toString() + ")"
    }
    else{
      render result.toString()
    }
  }

}
